package com.zut.hospital.controller.pharmacy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.dao.pharmacy.PurchaseMapper;
import com.zut.hospital.dao.pharmacy.SupplierMapper;
import com.zut.hospital.entity.pharmacy.Purchase;
import com.zut.hospital.entity.pharmacy.PurchaseExample;
import com.zut.hospital.entity.pharmacy.Supplier;
import com.zut.hospital.entity.pharmacy.SupplierExample;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.service.pharmacy.PurchaseService;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hos-test
 * @description: 药房
 * @author: 杨曼珍
 * @create: 2019-05-28 15:29
 **/
@Controller
@RequestMapping("/hospital")
public class PurchaseController {
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    PurchaseMapper purchaseMapper;


    private static final Logger log = LoggerFactory.getLogger(PurchaseController.class);
    @ResponseBody
    @RequestMapping(value = "/deletePurchase",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String deletePurchase(@RequestBody String data) {
        JSONObject jsonObject=JSONObject.parseObject(data);
        String key=jsonObject.getString("key");
        JSONArray jsonArray=JSONArray.parseArray(key);

        for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
        String number=jsonObject1.getString("number");
           log.info("number:"+number);
        int a=purchaseService.deletePurchase(number);
        if (a==0){
            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 0);
            layuiMap.put("msg", "failed");
            layuiMap.put("count", 0);
            layuiMap.put("data", null);
            return JSON.toJSONString(layuiMap);
        }
        }

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }

    @ResponseBody
    @RequestMapping(value = "/page")

    public String show(@RequestParam("page")int page,
                       @RequestParam("limit")int limit) {
       String name=request.getParameter("name");
       log.info("name:"+name);
if (name==null||name==""){
        return purchaseService.pagePurchase(page,limit);

}
else
    return purchaseService.findPurchaseByMedName(page,limit,name);

    }

/*    @ResponseBody
    @RequestMapping(value = "/page")

    public String show1(int page, int limit,String name) {
        return purchaseService.findPurchaseByMedName(page,limit,name);

    }*/
    @ResponseBody
    @RequestMapping(value = "/deletePurchase1",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String deletePurchase1(@RequestBody String data) {
        String number = JSONObject.parseObject(data).getString("number");
            log.info("number:"+number);
            int a=purchaseService.deletePurchase(number);
            if (a==0){
                Map<String, Object> layuiMap = new HashMap<>();
                layuiMap.put("code", 0);
                layuiMap.put("msg", "failed");
                layuiMap.put("count", 0);
                layuiMap.put("data", null);
                return JSON.toJSONString(layuiMap);
            }

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }

    @ResponseBody
    @RequestMapping(value = "/updatePurchase",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String updatePurchase(@RequestBody String data) {

        int id = JSONObject.parseObject(data).getInteger("id");


        String field = JSONObject.parseObject(data).getString("field");
        String value = JSONObject.parseObject(data).getString("value");

        purchaseService.updatePurchase(id,field,value);

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }
    @ResponseBody
    @RequestMapping(value = "/insertPurchase",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String insertPurchase1(@RequestBody String data,HttpSession session) throws ParseException {

        Date nowTime = new Date();
        /*System.out.println(nowTime);
        String number=new SimpleDateFormat("yyyy-MM-dd").format(nowTime);*/
        String medName = JSONObject.parseObject(data).getString("medName");
        int count = Integer.parseInt(JSONObject.parseObject(data).getString("count"));
        Double price = Double.parseDouble(JSONObject.parseObject(data).getString("price"));
        int sup = Integer.parseInt(JSONObject.parseObject(data).getString("sup"));
        String other = JSONObject.parseObject(data).getString("other");

        SupplierExample supplierExample=new SupplierExample();
        supplierExample.createCriteria().andIdEqualTo(sup);
        Supplier supplier=supplierMapper.selectByExample(supplierExample).get(0);
        String user=session.getAttribute("adminUser").toString();
        log.info(session.getAttribute("adminUser").toString());
        int a=purchaseService.insertPurchase(medName,price,count,nowTime,other,supplier.getSupName(),user);
        if (a==0){
            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 0);
            layuiMap.put("msg", "failed");
            layuiMap.put("count", 0);
            layuiMap.put("data", null);
            return JSON.toJSONString(layuiMap);
        }

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }
   /* @GetMapping(value = "/test")
    public ModelAndView test(HttpServletRequest req) {
        // UserEntity userEntity = getCurrentUser(req);
        User user = new User();
        user.setAdName("tom");
        user.setId(234);
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", user);
        mv.setViewName("/index.html");
        return mv;
    }*/

 /*   @ResponseBody
    @RequestMapping(value = "/page1",produces = "application/json;charset=utf-8",method = RequestMethod.POST)

    public String findByMedName(@RequestBody String data) throws IOException {
        String name = JSONObject.parseObject(data).getString("med_name");
        return purchaseService.findPurchaseByMedName(name);

    }*/
}
